<?php
include_once "Expense_DAO.php";

class Expense extends Expense_DAO{
	/**
	 * Return the full name of the user account to which 
	 * this expense is attached
	 * @return string
	 */
	public function getHolder(){
		return $this->getUser_id_Ref()->getFullName();
	}
	
	/**
	 * Returns the label for the expense type
	 * @return string
	 */
	public function getExpenseTypeLabel(){
		return $this->getExpensetype_id_Ref()->getLabel();
	}
	
/**
	 * Return the total value of the expenses (all the users)
	 * @return float a numeric value 
	 */
	public static function getTotalExpenses(){
		try {
			$l_dbh = new PDO(__DB_URL, __DB_USER, __DB_PASSWORD);
			$l_sth = $l_dbh->prepare("SELECT SUM(amount) as total FROM expense");
			if(!$l_sth->execute()){
				throw new PDOException(implode(" - ",$l_sth->errorInfo()));
			}
			$l_row=$l_sth->fetch(PDO::FETCH_ASSOC);
			$l_dbh = null;
			if(isset($l_row['total'])) return $l_row['total'];
			return 0;
		} catch (PDOException $e) {
			print "Error ! : " . $e->getMessage() . "<br/>";
			die();
		}
	}

	/**
	 * Return the total value of the expenses of the specified user
	 * @param $p_userId string a User ID
	 * @return float a numeric value 
	 */
	public static function getTotalExpensesForUser($p_userId){
		try {
			$l_dbh = new PDO(__DB_URL, __DB_USER, __DB_PASSWORD);
			$l_sth = $l_dbh->prepare("SELECT SUM(amount) as total FROM expense WHERE User_id=:User_id");
			$l_sth->bindParam(':User_id',$p_userId);
			if(!$l_sth->execute()){
				throw new PDOException(implode(" - ",$l_sth->errorInfo()));
			}
			$l_row=$l_sth->fetch(PDO::FETCH_ASSOC);
			$l_dbh = null;
			if(isset($l_row['total'])) return $l_row['total'];
			return 0;
		} catch (PDOException $e) {
			print "Error ! : " . $e->getMessage() . "<br/>";
			die();
		}
	}
}
?>